package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.StuWorkDao;
import com.ketangpai.admin.entity.StuWork;
import com.ketangpai.admin.entity.UserNotice;
import com.ketangpai.admin.entity.Work;
import com.ketangpai.admin.entity.dto.StuWorkDTO;
import com.ketangpai.admin.entity.param.StuWorkParam;
import com.ketangpai.admin.service.StuWorkService;
import com.ketangpai.admin.service.UserNoticeService;
import com.ketangpai.admin.service.WorkService;
import com.ketangpai.admin.utils.AliOSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生作业表(StuWork)表服务实现类
 *
 * @author makejava
 * @since 2023-06-14 02:10:41
 */
@Service("stuWorkService")
@Transactional
public class StuWorkServiceImpl extends ServiceImpl<StuWorkDao, StuWork> implements StuWorkService {

    @Resource
    private StuWorkDao dao;
    @Resource
    private AliOSSUtil aliOSSUtil;

    @Resource
    private UserNoticeService userNoticeService;
    @Resource
    private WorkService workService;

    @Override
    public StuWork queryById(Integer id) {
        return dao.selectById(id);
    }

    @Override
    public List<StuWork> queryByUid(Integer uid) {
        HashMap<String, Object> map = new HashMap<>();
        //自定义查询
        map.put("user_id", uid);
        return dao.selectByMap(map);
    }

    /**
     * 根据用户、课程id查询作业
     */
    @Override
    public List<StuWorkDTO> queryByUCid(Integer cid, Integer uid) {
        return dao.queryByUCid(cid, uid);
    }

    /**
     * 全部催交
     */
    @Override
    public boolean expediteBatch() {
        List<StuWork> list = dao.selectList(null);
        for (StuWork stuWork : list) {
            if (stuWork.getStatus() == 0) {
                addNotice(stuWork);
                stuWork.setExpediteCount(stuWork.getExpediteCount() + 1);
            }
        }
        return updateBatchById(list);
    }

    /**
     * 获取列表
     */
    public List<StuWorkDTO> getList(StuWorkParam param){
        param.setPage((param.getPage()-1)* param.getPageSize());
        System.out.println("getList"+dao.queryAllByLimit(param));
        return dao.queryAllByLimit(param);
    }
    /**
     * 返回给前端列表信息
     */
    public Map<String, Object> selectList(StuWorkParam param) {
        System.out.println("查询参数: " + param);
        List<StuWorkDTO> list =getList(param);
        System.out.println("查询结果: " + list);
        Integer workId = param.getWorkId();
        Map<String, Object> map = new HashMap<>();
        map.put("allTotal", dao.getAllTotal(param));
        map.put("total", dao.getTotal(param));
        map.put("noSubmitTotal", dao.countStatus(workId, 0) + dao.countStatus(workId, 3));
        map.put("noCheckTotal", dao.countStatus(workId, 1));
        map.put("checkTotal", dao.countStatus(workId, 2));
        map.put("list", list);
        System.out.println("stuWork的map:"+map);
        return map;
    }
    /**
     * 新增
     */
    @Override
    public boolean insert(StuWork stuWork) {
        if (stuWork.getFile() != null) {
            String url = aliOSSUtil.upload(stuWork.getFile());
            stuWork.setFilename(url);
        }
        return dao.insert(stuWork) > 0;
    }

    /**
     * 催交作业
     */
    @Override
    public boolean expedite(Integer id) {
        StuWork stuWork=dao.selectById(id);
        addNotice(stuWork);
        stuWork.setExpediteCount(stuWork.getExpediteCount() + 1);
        return update(stuWork);
    }

    /**
     * 用户通知新增
     */
    public void addNotice(StuWork stuWork) {
        Work work = workService.queryById(stuWork.getWorkId());
        UserNotice userNotice = new UserNotice();
        userNotice.setUserId(stuWork.getUserId());
        userNotice.setActivity(work.getActivity());
        userNotice.setContentId(stuWork.getId());
        userNotice.setCourseId(work.getCourseId());
        userNoticeService.save(userNotice);
    }

    /**
     * 修改
     */
    @Override
    public boolean update(StuWork stuWork) {
        if (stuWork.getFile() != null) {
            String url = aliOSSUtil.upload(stuWork.getFile());
            stuWork.setFilename(url);
        }
        return dao.updateById(stuWork) > 0;
    }

    /**
     * 删除
     */
    @Override
    public boolean deleteById(Integer id) {
        return dao.deleteById(id) > 0;
    }
}

